<template>
  <div id="community">
    <el-table :data="detail.list" style="width: 100%">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="id" v-if="false" />
      <el-table-column prop="name" label="树名" />
      <el-table-column prop="number" label="数量" />
      <el-table-column align="right">
        <template #header>
          <el-input
            v-model="search"
            size="small"
            placeholder="Type to search"
          />
        </template>
        <template #default="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
            修改
          </el-button>
          <el-dialog v-model="dialogFormVisible" title="修改信息">
            <UpdatePlant :current="current"></UpdatePlant>
          </el-dialog>
          <el-popconfirm
          title="你确定要删除该选项吗？"
          @confirm="confirmEvent"
          @cancel="cancelEvent"
        >
          <template #reference>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.$index)"
            >
              删除
            </el-button>
          </template>
        </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination  v-if="this.detail.total > 5"
      v-model:currentPage="detail.pageNum"
      v-model:page-size="detail.pageSize"
      :total="detail.total"
      :page-sizes="[5, 10]"
      :small="small"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    ></el-pagination>
    <el-dialog v-model="dialogFormVisible" title="显示图表">
      <Echarts :list = this.detail.list></Echarts>
    </el-dialog>
  </div>
  <el-button type="primary" plain @click="char">查看图表</el-button>
  <el-dialog v-model="dialogFormVisible2" title="显示图表">
    <Plantecharts :list="this.detail.list"></Plantecharts>
  </el-dialog>
</template>

<script>
import UpdatePlant from "./update-plant.vue";
import Plantecharts from "../../../main/plantecharts.vue";
export default {
  name: "plant",
  data() {
    return {
      detail: [
        {
          list: [],
          total: 0,
          pageNum: 0,
          pageSize: 0,
        },
      ],
      user: {},
      current: {},
      dialogFormVisible: false,
      dialogFormVisible2: false
    };
  },
  methods: {
    handleEdit(index, row) {
      this.current = row;
      this.dialogFormVisible = true;
    },
    confirmEvent() {
      this.$axios({
        method: "post",
        url: "http://localhost:8080///nav/tylir/communitythree/delete",
        data: this.$qs.stringify(this.detail.list[this.index]),
      }).then((res) => {
        if (res.data.code == 1) {
          ElMessage(res.data.msg);
        } else {
          console.log(res.data.msg);
        }
      });
    },
    handleDelete(index) {this.index = index},
    handleSizeChange(size) {
      this.detail.pageSize = size;
      this.mount();
    },
    handleCurrentChange(num) {
      this.pageNum = num;
      this.mount();
    },
    mount() {
      this.$axios({
        method: "post",
        url: "http://localhost:8080/nav/tylir/communitythree",
        data:
          this.$qs.stringify(this.user) +
          "&" +
          this.$qs.stringify({
            pageSize: this.detail.pageSize,
            pageNum: this.detail.pageNum,
          }),
      }).then((res) => {
        console.log(res.data);
        this.project2 = res.data;
        if (res.data.code == 1) {
          this.detail.list = res.data.data.list;
          this.detail.pageSize = res.data.data.pageSize;
          this.detail.pageNum = res.data.data.pageNum;
          this.detail.total = res.data.data.total;
        } else {
          console.log("error");
        }
      });
    },
    char(){
      this.dialogFormVisible2 = true;
    }
  },
  mounted() {
    this.user = this.$store.state.user;
    this.mount();
  },
  components: { UpdatePlant, Plantecharts },
};
</script>

<style scoped>
</style>